<?php

namespace frontend\modules\custom\controllers;

use common\models\db\UserRegisterInviteCode;
use frontend\modules\custom\models\forms\Custom;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;

class DefaultController extends Controller
{
    public function actionFztUserRegister($code)
    {
        $inviteCode = UserRegisterInviteCode::findOne(['code' => $code]);
        if (!$inviteCode) {
            throw new ForbiddenHttpException("无权访问");
        }
        if ($inviteCode->created_at + $inviteCode->period < YII_BT_TIME) {
            throw new ForbiddenHttpException("该链接已过期");
        }
        $fzt = \Yii::$app->fenZhangTong;
        $model = new Custom();
        $model->code = $code;
        $model->plat_no = $fzt->platNo;
//        $model->return_url = \Yii::$app->urlManagerFztadmin->baseUrl;
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            if ($model->register($fzt)) {
                $retUrl = $fzt->resp_arr['retUrl']??$model->return_url;
                return $this->redirect($retUrl);
            }
        }
        return $this->render('fzt-user-register', ['model' => $model, 'inviteCode' => $inviteCode]);
    }
}